package cn.edu.tju.core.security.repository;

import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import cn.edu.tju.core.model.User;

import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {

   @EntityGraph(attributePaths = "authorities")
   Optional<User> findOneWithAuthoritiesByUsername(String username);

   // 添加根据ID查询用户及其权限的方法
   @EntityGraph(attributePaths = "authorities")
   @Query("SELECT u FROM User u WHERE u.id = :userId")
   Optional<User> findOneWithAuthoritiesById(@Param("userId") Long userId);

}